package com.fate.system.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.framework.dto.IdParamDTO;
import com.fate.framework.dto.log.LogAddDTO;
import com.fate.framework.dto.log.LogPageDTO;
import com.fate.framework.dto.log.LogUpdateDTO;
import com.fate.framework.entity.Log;
import com.fate.framework.service.ILogService;
import com.fate.framework.utils.SystemUtil;
import com.fate.framework.vo.log.LogDetailVO;
import com.fate.framework.vo.log.LogListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "日志操作 API")
@RestController
@RequestMapping("/api/v1.0/log")
public class LogController {

  @Resource private ILogService logService;

  @SaCheckRole("super_admin")
  @Operation(summary = "新增")
  @PostMapping("/add")
  public void add(@RequestBody @Valid LogAddDTO addDTO) {
    Log log = SystemUtil.buildResource(addDTO, Log.class);
    logService.save(log);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "删除")
  @PostMapping("/remove")
  public void remove(@RequestBody @Valid IdParamDTO idParamDTO) {
    logService.removeById(idParamDTO.getId());
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "更新")
  @PostMapping("/update")
  public void update(@RequestBody @Valid LogUpdateDTO updateDTO) {
    Log log = SystemUtil.buildResource(updateDTO, Log.class);
    logService.updateById(log);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "详情")
  @PostMapping("/detail")
  public LogDetailVO detail(@RequestBody @Valid IdParamDTO idParamDTO) {
    Log log = logService.getById(idParamDTO.getId());
    return SystemUtil.buildResource(log, LogDetailVO.class);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "分页列表")
  @PostMapping("/getPageList")
  public Page<LogListVO> getPageList(@RequestBody @Valid LogPageDTO pageDTO) {
    Page<Log> page = logService.getPageList(pageDTO);
    return SystemUtil.buildResource(page, LogListVO.class);
  }
}
